import { createRouter, createWebHashHistory } from 'vue-router'
import Layout from '@/views/Layout.vue'
import Login from '@/views/Login.vue'
import store from '@/store'

const routes = [
  // {
  //   path: '/',
  //   redirect: '/login'
  // },
  {
    path: '/',
    name: 'home',
    meta: { title: '首页' },
    component: Layout,
    redirect: '/users',
    children: [
      // { path: '/', component: () => import('@/views/home') },
      { path: 'users', component: () => import('@/views/user'), meta: { title: '用户列表' } },
      { path: 'roles', component: () => import('@/views/roles'), meta: { title: '角色列表' } },
      { path: 'rights', component: () => import('@/views/rights'), meta: { title: '权限列表' } },
      { path: 'goods', component: () => import('@/views/goods'), meta: { title: '商品列表' } },
      { path: 'goods/add', name: 'add', component: () => import('@/views/goods/AddGoods'), meta: { title: '添加商品' } }
    ]
  },
  {
    path: '/login',
    name: 'login',
    component: Login
  }
]

const router = createRouter({
  history: createWebHashHistory(),
  routes
})

// 路由导航守卫
// const whiteList = ['/login']
router.beforeEach((to, from, next) => {
  const { token } = store.state
  if (token) {
    if (to.path === '/login') {
      next()
    } else {
      next()
    }
  } else {
    if (to.path === '/login') {
      next()
    } else {
      next('/login')
    }
  }
})

export default router
